package com.example.photoalbum.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import com.example.util.Result;
import com.example.util.PageResult;
import com.example.photoalbum.service.IPhotoalbumService;
import com.example.photoalbum.entity.Photoalbum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;


/**
 * <p>
 * 相册信息表 前端控制器
 * </p>
 * @author Jiamingzhen
 * @CreateDate 2021-04-23 ${time}
 * @copyright:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/photoalbum/photoalbum")
public class PhotoalbumController{

    private String PREFIX = "view/photoalbum/";

    @Autowired
    private IPhotoalbumService photoalbumService;
    /**
     * 跳转到相册信息表首页
     */
    @RequestMapping("")
    public String index(Model model,HttpServletRequest request) {
        return PREFIX + "photoalbum";
    }

    /**
     * 跳转到添加相册信息表
     */
    @RequestMapping("/photoalbumadd")
    public String toAdd(Model model,HttpServletRequest request) {
        return PREFIX + "photoalbum_add";
    }

    /**
     * 跳转到修改相册信息表
     */
    @RequestMapping("/photoalbumupdate/{photoalbumId}")
    public String toUpdate(Model model,HttpServletRequest request,@PathVariable("photoalbumId") String photoalbumId) {
        Photoalbum photoalbum = photoalbumService.getById(photoalbumId);
        model.addAttribute("photoalbum",JSONObject.toJSON(photoalbum));
        return PREFIX + "photoalbum_edit";
    }


    /**
     * 相册信息表详情
     */
    @RequestMapping(value = "/detail/{photoalbumId}")
    public String toDetail(@PathVariable("photoalbumId") String photoalbumId, Model model,HttpServletRequest request) {
        return PREFIX + "photoalbum_view";
    }

    /**
     * 获取相册信息表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public PageResult<Photoalbum> list(Integer page, Integer limit, Photoalbum photoalbum,String condition, Model model,HttpServletRequest request) {
        return photoalbumService.listPage(page,limit,photoalbum);
    }


    /**
     * 新增相册信息表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result add(Photoalbum photoalbum) {
        if (photoalbumService.save(photoalbum)) {
            return Result.success(200,"添加成功");
        } else {
            return Result.error(500,"添加失败");
        }
    }

    /**
     * 删除相册信息表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam String photoalbumId) {
        if (photoalbumService.removeById(photoalbumId)) {
            return Result.success(200,"删除成功");
        } else {
            return Result.error(500,"删除失败");
        }
    }

    /**
     * 逻辑删除相册信息表
     */
    @RequestMapping(value = "/logicDelete")
    @ResponseBody
    public Result logicDelete(@RequestParam String photoalbumId) {
        Photoalbum photoalbum=photoalbumService.getById(photoalbumId);
        if (photoalbumService.logicDelete(photoalbum)) {
            return Result.success(200,"删除成功");
        } else {
            return Result.error(500,"删除失败");
        }
    }


    /**
     * 修改相册信息表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(Photoalbum photoalbum) {
        if (photoalbumService.updateById(photoalbum)) {
            return Result.success(200,"修改成功");
        } else {
            return Result.error(500,"删除失败");
        }
    }


    @RequestMapping("/batchDelete")
    @ResponseBody
    public Result batchDelete(String departIds) {
        System.out.println(departIds);
        List<String> lis = Arrays.asList(departIds.split(","));
        if (photoalbumService.removeByIds(lis)) {
            return Result.success(200,"删除成功");
        }
        return Result.error(500,"删除失败");
    }
}
